import Vuex from 'vuex';
import Vue from 'vue';

Vue.use(Vuex);

let store = new Vuex.Store({
    state: {
        location: {
            lat: 0,
            lng: 0,
            address: "",
            city: ''
        },
        states: 'turn-on',
        userInfo: localStorage.getItem('x-user') ? JSON.parse(localStorage.getItem('x-user')) : null,
        socket: null

    },
    mutations: {

        setTransition(state, payload) {
            state.states = payload
        },

        //更新定位信息
        updateLocation(state, payload) {
            state.location.lat = payload.lat
            state.location.lng = payload.lng
            if (payload.address) {
                state.location.address = payload.address
            }
            if (payload.city) {
                state.location.city = payload.city
            }
        },
        updateSocket(state, payload) {
            state.socket = payload.socket
        },

        updateAddress(state, payload) {
            state.address = payload.address
        },
        saveUserInfo(state, payload) {
            state.userInfo = payload.userInfo
            localStorage.setItem('x-user', JSON.stringify(payload.userInfo))
        },
        clearUserInfo(state) {
            state.userInfo = null
            localStorage.removeItem('x-user')
        }
    },
    actions: {
        handleSocketMessage(context, data) {
            console.log(data);
            if (data.mid == 2007) {
                window.eventBus.$emit('onShareOrderCancelByUserResult', data.data)
            } else if (data.mid == 2011) {
                window.eventBus.$emit('onDriverGetShareOrderResult', data.data)
            } else if (data.mid == 2021) {
                window.eventBus.$emit("onShareDriverLocationUpdate",data.data)
            } else if (data.mid == 2015) {
                window.eventBus.$emit("onShareUserStart",data.data)

            }
        },
        socketLogin(context) {
            context.state.socket.send(JSON.stringify({
                mid: 21, data: {
                    uid: context.state.userInfo.passenger.passengerid,
                    type: 0,
                    loginType: 0
                }
            }))
        }
    }
})

export default store;